package com.ibeeking.found.org.cp.rest.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ibeeking.found.org.cp.rest.service.IEmployeeFieldService;
import com.ibeeking.found.org.cp.rest.service.IEmployeeService;
import com.ibeeking.found.org.service.common.dos.EmployeeFieldOptionsDO;
import com.ibeeking.found.org.service.common.dos.EmployeeFieldDO;
import com.ibeeking.found.org.service.common.enums.EmployeeFieldDataTypeEnum;
import com.ibeeking.found.org.service.common.vo.EmployeeFieldOptionsItemParamVO;
import com.ibeeking.found.org.service.common.vo.EmployeeFieldVO;
import com.ibeeking.found.org.service.mapper.EmployeeFieldOptionsMapper;
import com.ibeeking.found.org.service.mapper.EmployeeFieldMapper;
import com.ibeeking.nematos.utils.bean.BeanUtil;
import com.ibeeking.nematos.utils.data.Collections3;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName EmployeeFieldServiceImpl
 * @Description
 * @Author ibeeking
 * @Date 2022-01-24 11:39
 **/
@Service
public class EmployeeFieldServiceImpl extends ServiceImpl<EmployeeFieldMapper, EmployeeFieldDO> implements IEmployeeFieldService {

    @Resource
    private EmployeeFieldMapper employeeFieldMapper;

    @Resource
    private EmployeeFieldOptionsMapper employeeFieldOptionsMapper;

    @Resource
    private IEmployeeService employeeService;

    @Override
    public List<EmployeeFieldVO> fieldListQuery(Long memberId) {
        LambdaQueryWrapper<EmployeeFieldDO> queryWrapper = new LambdaQueryWrapper<>();
        // 只查询自定义字段
        queryWrapper.eq(EmployeeFieldDO::getFieldType,1);
        queryWrapper.orderByDesc(EmployeeFieldDO::getCreateTime);
        JSONObject jsonObject = employeeService.queryEmployeeExtendField(memberId);

        List<EmployeeFieldDO> list = employeeFieldMapper.selectList(queryWrapper);
        List<EmployeeFieldVO> result = new ArrayList<>();
        if (CollUtil.isNotEmpty(list)) {
            for (EmployeeFieldDO item : list) {
                EmployeeFieldVO fieldVO = wrapperVo(item);
                Integer dataType = fieldVO.getDataType();
                List<Integer> boxCodeList = Arrays.asList(EmployeeFieldDataTypeEnum.CHECK_BOX_MANY.getCode(), EmployeeFieldDataTypeEnum.CHECK_BOX_ONE.getCode());
                if (boxCodeList.contains(dataType)) {
                    wrapperVoFieldOptions(fieldVO);
                }
                fieldVO.setValue(jsonObject.get(fieldVO.getCode()));
                result.add(fieldVO);
            }
        }
        return result;
    }

    private EmployeeFieldVO wrapperVo(EmployeeFieldDO fieldDo) {
        if (ObjectUtil.isNull(fieldDo)) {
            return new EmployeeFieldVO();
        }
        EmployeeFieldVO fieldVo = BeanUtil.convertBean(fieldDo, EmployeeFieldVO.class);
        fieldVo.setDataTypeName(EmployeeFieldDataTypeEnum.getName(fieldVo.getDataType()));
        return fieldVo;
    }

    private void wrapperVoFieldOptions(EmployeeFieldVO vo) {
        if (ObjectUtil.isNull(vo)) {
            return;
        }

        LambdaQueryWrapper<EmployeeFieldOptionsDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EmployeeFieldOptionsDO::getFieldId, vo.getId());
        List<EmployeeFieldOptionsDO> fieldOptionsDoList = employeeFieldOptionsMapper.selectList(queryWrapper);

        if (Collections3.isNotEmpty(fieldOptionsDoList)) {
            vo.setOptionsItemList(fieldOptionsDoList.stream().map(x -> new EmployeeFieldOptionsItemParamVO()
                    .setOptionsValue(x.getOptionsValue())
                    .setOptionsMsg(x.getOptionsMsg())
                    .setChecked(x.getChecked())
            ).collect(Collectors.toList()));
        }
    }
}
